// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Xml.Serialization;
using BlueOnionSoftware.Bloget.Utility;

namespace BlueOnionSoftware.Bloget.Providers.Data
{
    [Serializable]
    [XmlRoot("roles", Namespace = RoleNamespace)]
    public class RoleCollection : Collection<Role>
    {
        internal const string RoleNamespace = "urn:blueonionsoftware-com:bloget:role:data";

        public RoleCollection()
        {
        }

        internal RoleCollection(IList<Role> roles)
            : base(roles)
        {
        }

        public static RoleCollection Load(string fileName)
        {
            try
            {
                return Serializer.Load<RoleCollection>(fileName, RoleNamespace);
            }

            catch (FileNotFoundException)
            {
                return new RoleCollection();
            }
        }

        public void Save(string fileName)
        {
            Serializer.Save<RoleCollection>(fileName, RoleNamespace, this);
        }

        public Role FindByName(string name)
        {
            foreach (var role in this)
            {
                if (string.Compare(role.Name, name, true, CultureInfo.CurrentCulture) == 0)
                {
                    return role;
                }
            }

            return null;
        }
    }
}